package com.fhao.forum.services.impl;

import com.fhao.forum.common.AppResult;
import com.fhao.forum.common.ResultCode;
import com.fhao.forum.dao.BoardMapper;
import com.fhao.forum.exception.ApplicationException;
import com.fhao.forum.model.Board;
import com.fhao.forum.services.IBoardService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        //参数校验
        if(num == null || num <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Board> result = boardMapper.selectByNum(num);
        //service层直接返回结果,不对结果进行参数校验,controller层再对结果进行校验.
        return result;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //参数校验
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        //对 拿到的版块进行校验
        if(board == null) {
            log.warn("BoardServiceImpl 的更新帖子数量" + ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //重新new一个版块,调用mapper中的动态更新方法,对版块进行动态更新(空值不更新)
        Board updateBoard = new Board();
        updateBoard.setId(board.getId()); //设置要更新的属性
        updateBoard.setArticleCount(board.getArticleCount()+1); //设置要更新的属性
        //更新数据库
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);//调用动态更新的方法
        if(row != 1) {
            log.warn(ResultCode.FAILED.toString()+"受影响的行数不等于1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        //参数校验,判断非空
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //拿到版块
        Board board = boardMapper.selectByPrimaryKey(id);
        //版块校验
        if(board == null) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString()+"boardId = "+id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()-1);
        if(updateBoard.getArticleCount() < 0) {
            updateBoard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES.toString()));
        }
    }

    @Override
    public Board selectById(Long id) {
        if(id == null || id <= 0) {
            log.warn("BoardServiceImpl 根据id查询版块信息 "+ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //返回查询结果
        return boardMapper.selectByPrimaryKey(id);
    }
}

















